package org.osiris.server.engine;

import java.security.Security;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SMSNotificacion implements NotificacionInterface {

	
	private String asunto;
	private String cuerpoPlano;
	private String cuerpoHTML;
	private String destinatario;
	
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	public SMSNotificacion(String pAsunto, String pTextoPlano,String pTextoHTML,String pDestinatario){
		this.asunto=pAsunto;
		this.destinatario=pDestinatario;
		this.cuerpoHTML=pTextoHTML;
		this.cuerpoPlano=pTextoPlano;
	}
	public boolean notificar() {
		try{
			System.out.println("sendMail()...");
			Properties props = new Properties();
			props.put("mail.smtp.host", SMTP_HOST_NAME);
			props.put("mail.smtp.auth", "true");
	        props.put("mail.smtps.auth", "true");
			props.put("mail.debug", "true");
			props.put("mail.transport.protocol", "smtps");
		    props.put("mail.smtp.starttls.enable", "true"); // added this line
			props.put("mail.smtp.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.put("mail.smtp.socketFactory.fallback", "false");
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			Session session = Session.getInstance(props);
			session.setDebug(true);
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress("osirisprojectupm@gmail.com"));
			
			Object obj = new InternetAddress(destinatario);
			
			message.addRecipient(Message.RecipientType.TO, (Address)obj);
			
			System.out.println("sendMail() 2...");

			message.setSubject(asunto);
			message.setContent(cuerpoPlano, "text/plain");

			Transport transport = session.getTransport();

			//***** GET STUCK HERE!!! ******
			transport.connect(SMTP_HOST_NAME, 465, "osirisprojectupm@gmail.com", "123456qwerty");

			System.out.println("sendMail() 3...");

			transport.sendMessage(message,  message.getRecipients(Message.RecipientType.TO));

			System.out.println("sendMail() 4...");
		}catch(Exception ex){
			ex.printStackTrace();
			return false;}

		return true;
		

	}



public void setAsunto(String asunto) {
		this.asunto = asunto;
	}
public String getAsunto() {
		return asunto;
	}
public void setTextoPlano(String textoPlano) {
		this.cuerpoPlano = textoPlano;
	}
public String getTextoPlano() {
		return cuerpoPlano;
	}



public void setTextoHTML(String textoHTML) {
	this.cuerpoHTML = textoHTML;
}



public String getTextoHTML() {
	return cuerpoHTML;
}



public void setDestinatario(String destinatario) {
	this.destinatario = destinatario;
}



public String getDestinatario() {
	return destinatario;
}

}
